/***********************************************************
 * 开票单
 * *******************************************************/
 
// 开票单 选择单据的回调函数
function kpQueryFunc(){
	$('#khk_dataQuery').attr('onclick','kpQuery(this)');
	var allRows=$('#khk_GEN_DATALIST').datagrid('getRows');
	var oldRows = $('#cbg_addItemDatagrid').datagrid('getRows');
	for(var i = 0;i<allRows.length;i++){
		for(var j = 0;j<oldRows.length;j++){
			if(allRows[i].SOURCE_ID == oldRows[j].JS_ITEM_ID){
				$('#khk_GEN_DATALIST').datagrid('deleteRow',i);
			}
		}
	}
}
//开票单的自动金额方法 (按钮已经隐藏,暂时不使用)
function kpautoMoney(obj){
	var prefix = $(obj).attr('pre');
	var allRows = $('#'+prefix+'_addItemDatagrid').datagrid('getRows');
	if(allRows.length==0){
		$.messager.alert('提示','没有明细数据，无法自动金额！');
	}else{
		for(var i = 0;i<allRows.length;i++){
			$('#'+prefix+'_addItemDatagrid').datagrid('endEdit',i);
			allRows[i].NOW_MONEY = allRows[i].NEED_MONEY;
			$('#'+prefix+'_addItemDatagrid').datagrid('beginEdit',i);
			$('#'+prefix+'_addItemDatagrid').datagrid('endEdit',i);
			$('#'+prefix+'_addItemDatagrid').datagrid('refreshRow',i);
		}
	}
}
//开票单 选择单据中的查询按钮调用的方法
function kpQuery(obj){
	var vendorId =$('#cbg_addWinTable input[qucln=JS_ID]').val();
	var prefix=$(obj).attr('pre');
	var dsId=$(obj).attr('dsid');
	var resultDG = 'GEN_DATALIST';
	var comUseConf='commUse';
	var comUse = parseConfig(prefix+"_"+comUseConf);
	var otherConfig='1-JS_ID-'+vendorId+'-inc|+';
	var useConf = otherConfig+comUse;
	$('#'+prefix+"_"+resultDG).datagrid({
		url:_m_url+'framework/loadDataQ',
		queryParams:{'dsId':dsId,'config':useConf},
		onLoadSuccess:function(){
			var dataRows = $('#'+prefix+"_"+resultDG).datagrid('getRows');
			var oldRows = $('#cbg_addItemDatagrid').datagrid('getRows');
			if(dataRows.length >0){
				for(var i =0;i<dataRows.length;i++){
					for(var j = 0;j<oldRows.length;j++){
						if(dataRows[i].JS_ITEM_ID == oldRows[j].SOURCE_ID){
							$('#khk_GEN_DATALIST').datagrid('deleteRow',i);
						}
					}
				}
			}
		}
	});
}
// 开发票弹出窗口的确定按钮
function _cbg_sure1(){
	var invId = $('#cbg_addWinTable input[qucln=INV_ID]').val();  		// 主表主键ID
	$('#khk_GEN_PARENT_PANEL').window('close');
	var selRows = $('#khk_GEN_DATALIST').datagrid('getSelections');
	var oldRows = $('#cbg_addItemDatagrid').datagrid('getRows');
	var oldlength = oldRows.length;
	var sortNum = getSortNum('cbg');									// 顺序号
	for ( var i = 0; i < selRows.length; i++) {
		$('#cbg_addItemDatagrid').datagrid('appendRow', {
			INV_ID:invId,
			INPUT_DATE:selRows[i].INPUT_DATE,
			INPUT_DATE:selRows[i].YW_INPUT_DATE,
			DJ_ID:selRows[i].YW_ID,
			DJ_CODE:selRows[i].YW_CODE,
			YW_ID:selRows[i].YW_ID,
			YW_CODE:selRows[i].YW_CODE,
			TOTAL_MONEY:selRows[i].TOTAL_MONEY,
			C_DISCOUNT:selRows[i].C_DISCOUNT,
			REAL_MONEY:selRows[i].REAL_MONEY,
			NOW_MONEY:selRows[i].NOW_MONEY,
			MEMO:selRows[i].YW_MEMO,
			SOURCE_ID:selRows[i].JS_ITEM_ID,
			THIS_NOW_MONEY:selRows[i].NOW_MONEY,
			SORT_NUMBER:sortNum+i+1
		})
	}
	//调用公式计算方法
	kpGsjsFunc();
}
//界面上对账单单号拉取明细的方法
function getDZItemData(obj){
	var jsId= $(obj).val();
	var invId = $('#cbg_addWinTable input[qucln=INV_ID]').val();  	// 主表主键ID
	var sortNum = getSortNum('cbg');								// 顺序号
	var oldRows = $('#cbg_addItemDatagrid').datagrid('getRows');
	var oldlength=oldRows.length;

	$.ajax({
		type:'post',
		url:_m_url+'fxa/getDZItemList',
		data:{'djId':jsId,'flag':'k'},
		success:function(data){
			if(oldlength == 0){
				for(var i = 0;i<data.length;i++){
					$('#cbg_addItemDatagrid').datagrid('appendRow', {
						INV_ID:invId,
						INPUT_DATE:data[i].INPUT_DATE,
						YW_INPUT_DATE:data[i].YW_INPUT_DATE,
						DJ_ID:data[i].YW_ID,
						DJ_CODE:data[i].YW_CODE,
						YW_ID:data[i].YW_ID,
						YW_CODE:data[i].YW_CODE,
						TOTAL_MONEY:data[i].TOTAL_MONEY,
						C_DISCOUNT:data[i].C_DISCOUNT,
						REAL_MONEY:data[i].REAL_MONEY,
						NOW_MONEY:data[i].NOW_MONEY,
						YW_MEMO:data[i].YW_MEMO,
						SOURCE_ID:data[i].JS_ITEM_ID,
						THIS_NOW_MONEY:data[i].NOW_MONEY,
						SORT_NUMBER:sortNum+i+1
					})
				}
			}else{
				for ( var j = 0; j < data.length; j++) {
					for(var k = 0; k < oldlength;k++){
						if(data[j].JS_ITEM_ID == oldRows[k].SOURCE_ID ){
							break;
						}else{
							if(k==(oldlength-1)){
								$('#cbg_addItemDatagrid').datagrid('appendRow', {
									INV_ID:invId,
									INPUT_DATE:data[j].INPUT_DATE,
									YW_INPUT_DATE:data[j].YW_INPUT_DATE,
									DJ_ID:data[j].YW_ID,
									DJ_CODE:data[j].YW_CODE,
									YW_ID:data[j].YW_ID,
									YW_CODE:data[j].YW_CODE,
									TOTAL_MONEY:data[j].TOTAL_MONEY,
									C_DISCOUNT:data[j].C_DISCOUNT,
									REAL_MONEY:data[j].REAL_MONEY,
									NOW_MONEY:data[j].NOW_MONEY,
									YW_MEMO:data[j].YW_MEMO,
									SOURCE_ID:data[j].JS_ITEM_ID,
									THIS_NOW_MONEY:data[j].NOW_MONEY,
									SORT_NUMBER:sortNum+j+1
								})
							}
						}
					}
				}
			}
		}
	});
	//调用公式计算方法
	kpGsjsFunc();
}
//界面金额计算方法
function kpGsjsFunc(){
	var bdje = 0;
	var allRows = $('#cbg_addItemDatagrid').datagrid('getRows');
	if(allRows.length == 0){
		bdje = 0.00;
	}else{
		for(var i =0;i<allRows.length;i++){
			if(allRows[i]["NOW_MONEY"] != null && allRows[i]["NOW_MONEY"] != ''){
				bdje = parseFloat(bdje) + parseFloat(allRows[i]["NOW_MONEY"]);
				bdje = (parseFloat(bdje)).toFixed(2);						 
			}
		}
	}
	//获取明细合计 本单金额
	$('#cbg_addWinForm').find('input[qucln=INVOICE_MONEY]').val(bdje);
	$('#cbg_addWinForm').find('input[qucln=INVOICE_MONEY]').attr('chan','T');
	$('#cbg_addWinForm').find('input[qucln=INVOICE_MONEY]').next().val(bdje);
}
//删除按钮
function fxkpRowDel(prefix){
	//删除一行
	itemRowDel(prefix); 
	//调用计算方法
	kpGsjsFunc();
}
//根据收款状态判断按钮
function pdskStatus(){
	 //获取当前单据的开票Id
	 var invId = $('#cbg_addWinTable input[qucln=INV_ID]').val();  	// 主表主键ID
	 //判断是否存在收款
	 $.ajax({
		 type:'post',
		 url:_m_url+'fxa/countByKPId',
		 data:{'invId':invId},
		 success:function(data){
			if(data >0){
				$('#cbg_M_UBUTN a[code=newSK]').linkbutton("disable");    //生成收款按钮 灰掉
			} else{
				$('#cbg_M_UBUTN a[code=newSK]').linkbutton("enable");   //显示
			}
		 }
	 })
}
//某些列是否显示
function columnShow(){
	//判断当前单据是否是审核
	 var isVerify = $('#cbg_addWinTable input[qucln=IS_VERIFY]').val();  //审核状态
	 var sureFlag = $('#cbg_addWinTable input[qucln=SURE_INV]').val();  //确认开票的状态
	 if(isVerify =='1' && sureFlag != '1'){ //如果是审核的,并且没有确认开票
		 $('#cbg_addWinTable input[qucln=INVOICE_NO]').parent().children('input[so=true]').remove();
		 $('#cbg_addWinTable input[qucln=INVOICE_NO]').css('display','block');
	 }
	 if(isVerify =='1' && sureFlag == '1'){ //如果是审核的，并且已经确认开票
		 $("#cbg_M_UBUTN a[code=newSK]").linkbutton("disable"); //确认开票的按钮灰掉
	 }
}
//确认开票按钮  
function sureKP(obj){
	var saveObj = $("#cbg_M_UBUTN a[id=cbg_SAVE_BUT]");
	var isVerify = $('#cbg_addWinTable input[qucln=IS_VERIFY]').val();  //审核状态
	var kpId = $('#cbg_addWinTable input[qucln=INV_ID]').val();  //主键
	var fpno = $('#cbg_addWinForm input[qucln=INVOICE_NO]').val();  //获取开票号值
	var iskp = $('#cbg_addWinForm input[qucln=KP_STATUS]').val();   //获取开票的值
	if (fpno == null || fpno =='') {
		$.messager.alert("温馨提示","亲，发票号不能为空。不能开票哦！！");
		return;
	}
	 if(isVerify !='1'){  
		 $.messager.alert("温馨提示","亲，当前单据没有审核不能进行本按钮操作！");
		 return;
	 }else{
		 //将当期日期自动填充到开票日期字段中
		 //获取开票日期
		 var kpDataObj=$("#cbg_addWinTable input[qucln=INVOICE_DATE]");
		 var kpDataObj=$("#cbg_addWinTable input[qucln=INVOICE_DATE]");
		 $(kpDataObj).attr("chan","T");
		 $(kpDataObj).val(getSmpFormatDate(new Date(),"false"));
		 $("#cbg_addWinTable input[qucln=INVOICE_DATE]").parent().find("input[so=true]").val(getSmpFormatDate(new Date(),"false"));
		 //先将界面上的发票号保存
		 common_save(saveObj);
		 //修改确认开票状态
		 $.ajax({
			 type:'post',
			 url:_m_url+'fxa/kpSureFunc',
			 data:{'kpId':kpId},
			 success:function(data){
				 if(data){
					 //获取主键
					 var pkStr = parsePKStr("cbg_addWinTable");
					 //刷新数据
					 endAppLoadDataM('ds_fx_invoice_kp1;ds_fx_invoice_kp_item1','cbg',pkStr);
					 var sureFlag = $('#cbg_addWinTable input[qucln=SURE_INV]').val();  //确认开票的状态
					 if(sureFlag == '1'){
						 //将界面上的列都只读
						 readOnlyForm("cbg_addWinTable");
						 $("#cbg_M_UBUTN a[code=newSK]").linkbutton("disable"); //确认开票的按钮灰掉
						 
					 }
				 }
			 }
		 });
		 var sureFlag = $('#cbg_addWinTable input[qucln=SURE_INV]').val();  //确认开票的状态
		 if(sureFlag == '1'){
			 kpsqzt();
		 }
	 }
	
}

/**
 * 
 */
function kpsqzt(){
	var saveObj = $("#cbg_M_UBUTN a[id=cbg_SAVE_BUT]");
	var isVerify = $('#cbg_addWinTable input[qucln=IS_VERIFY]').val();  //审核状态
	var kpId = $('#cbg_addWinTable input[qucln=INV_ID]').val();  //主键
	var fpno = $('#cbg_addWinForm input[qucln=INVOICE_NO]').val();  //获取开票号值
	var iskp = $('#cbg_addWinForm input[qucln=KP_STATUS]').val();   //获取开票的值
	if (fpno == null || fpno =='') {
		$.messager.alert("温馨提示","亲，发票号不能为空。不能开票哦！！");
		return;
	}
	 if(isVerify !='1'){  
		 $.messager.alert("温馨提示","亲，当前单据没有审核不能进行本按钮操作！");
		 return;
	 }else{
		 //将当期日期自动填充到开票日期字段中
		 //获取开票日期
		 var kpDataObj=$("#cbg_addWinTable input[qucln=INVOICE_DATE]");
		 var kpDataObj=$("#cbg_addWinTable input[qucln=INVOICE_DATE]");
		 $(kpDataObj).attr("chan","T");
		 $(kpDataObj).val(getSmpFormatDate(new Date(),"false"));
		 $("#cbg_addWinTable input[qucln=INVOICE_DATE]").parent().find("input[so=true]").val(getSmpFormatDate(new Date(),"false"));
		 //先将界面上的发票号保存
		 common_save(saveObj);
		 //修改确认开票状态
		 $.ajax({
			 type:'post',
			 url:_m_url+'fxa/kpsqzt',
			 data:{'kpId':kpId},
			 success:function(data){
				 if(data){
					 //获取主键
					 var pkStr = parsePKStr("cbg_addWinTable");
					 //刷新数据
					 endAppLoadDataM('ds_fx_invoice_kp1;ds_fx_invoice_kp_item1','cbg',pkStr);
				 }
			 }
		 });
	 }
	
} 

/**
 * 开票单生成收款单
 * 
 * @param obj
 * @return
 */
function kpNewSK(obj){
	var pre = $(obj).attr('pre');  // 获取前缀
	var kpId = $('#'+pre+'_addWinForm input[qucln=INV_ID]').val();
	var verify = $('#'+pre+'_addWinForm input[name=isVerify]').val();
	if(kpId == null || kpId ==""){
		$.messager.alert("温馨提示","单据尚未保存，不能生成开票单！");
		return;
	}
	if(verify !='1' || verify == null || verify ==''){
		$.messager.alert('提示','单据未审核，不能进行此操作！');
		return;
	}
	$.ajax( {
		type : 'post',
		url : _m_url+'newcwCost/kpNewSK',  
		data : {'kpId' :kpId},
		success : function(data) {
			if(data.info == 'fail'){
				$.messager.alert("提示",data.msg);
			}else{
				$.messager.alert("温馨提示","亲，收款单已生成");
				pdskStatus();
			}
		}
	});
}